package net.wexoo.group_cost_calculator.gui;

import java.util.List;

import net.wexoo.group_cost_calculator.R;
import net.wexoo.group_cost_calculator.dao.DBHelper;
import net.wexoo.group_cost_calculator.enitities.Calculation;
import net.wexoo.group_cost_calculator.enitities.CalculationMember;
import net.wexoo.group_cost_calculator.enitities.Person;
import net.wexoo.group_cost_calculator.gui.util.UIUtils;
import net.wexoo.group_cost_calculator.proxy.ProxyFactory;
import net.wexoo.group_cost_calculator.util.Log;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TextView;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;

public class ResultActivity extends OrmLiteBaseActivity<DBHelper> {

   private static final String TAG = ResultActivity.class.getSimpleName();

   private Calculation selectedCalculation;

   private Double sum;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.result);

      sum = 0.0;
   }

   @Override
   protected void onResume() {

      TableLayout tlResults = (TableLayout) findViewById(R.id.tl_results);
      LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

      selectedCalculation = ProxyFactory.getProxy(this).getSelectedCalculation();

      ((TextView) findViewById(R.id.tv_result_title)).setText(selectedCalculation.getTitle());
      ((TextView) findViewById(R.id.tv_result_date)).setText(UIUtils.getDateAsGermanString(selectedCalculation
            .getEditDate()));

      List<Person> persons = selectedCalculation.getMembers();

      for (Person person : persons) {
         sum += person.getPayedCosts();
      }

      for (int i = 0; i <= persons.size(); i++) {
         View resultTableRow = linflater.inflate(R.layout.result_table_row, null);

         // Set an id to access values later on
         TextView personNameText = (TextView) resultTableRow.findViewById(R.id.tv_result_name);
         personNameText.setId(personNameText.getId() + (i + 1000));
         personNameText.setText(i != persons.size() ? persons.get(i).getName() : getResources().getString(
               R.string.tv_result_payed_sum));

         TextView personPayedText = (TextView) resultTableRow.findViewById(R.id.tv_result_payed_costs);
         personPayedText.setId(personPayedText.getId() + (i + 2000));
         personPayedText.setText(i != persons.size() ? String.valueOf(persons.get(i).getPayedCosts()) : String
               .valueOf(sum));

         if (i == persons.size()) {
            tlResults.addView(linflater.inflate(R.layout.dividing_line, null));
         }

         TextView personOutstandingText = (TextView) resultTableRow.findViewById(R.id.tv_result_outstanding_costs);
         personOutstandingText.setId(personOutstandingText.getId() + (i + 3000));
         personOutstandingText.setText(i != persons.size() ? UIUtils.formatDoubleToTwoDecimalPlacesString(sum
               / persons.size() - persons.get(i).getPayedCosts()) : "");

         tlResults.addView(resultTableRow);
      }
      super.onResume();
   }

   public void processResult(View v) {
      switch (v.getId()){
         case R.id.b_save_result:
            ProxyFactory.getProxy(this).saveOrUpdateEntity(selectedCalculation, Calculation.class);

            ProxyFactory.getProxy(this).saveOrUpdateEntities(selectedCalculation.getMembers(), Person.class);

            for (Person person : selectedCalculation.getMembers()) {
               // Delete existing connections of calculation
               ProxyFactory.getProxy(this).saveOrUpdateEntity(
                     new CalculationMember(
                           selectedCalculation.getId(),
                           person.getId()), CalculationMember.class);
            }

            Log.d(TAG, "Calculation saved!");
            break;
      }
   }
}